import numpy as np  
import pandas as pd  
import tensorflow as tf  
from sklearn.model_selection import train_test_split  
from sklearn.preprocessing import StandardScaler  
from scipy.fft import fft  
  
# 假设已经准备好带钢产品的规格数据、工艺参数和硬度数据  
X = pd.read_csv('data.csv', usecols=[2, 6, 7, 9])  # 选取四个特征值  
y = pd.read_csv('data.csv', usecols=[12])         # 硬度数据  
  
# 数据预处理：标准化特征值  
scaler = StandardScaler()  
X_scaled = scaler.fit_transform(X)  
  
# 对每个特征应用傅里叶变换，并选择一部分频率成分  
n_features = X_scaled.shape[1]  
n_fft_features = 10  # 选择10个傅里叶特征  
fft_features = np.zeros((X_scaled.shape[0], n_features * n_fft_features))  
  
for i in range(n_features):  
    fft_vals = fft(X_scaled[:, i])  
    fft_features[:, i * n_fft_features:(i + 1) * n_fft_features] = np.abs(fft_vals[:n_fft_features])  
  
# 将原始的标准化特征与傅里叶特征合并  
X_combined = np.hstack((X_scaled, fft_features))  
  
# 划分数据集为训练集和测试集  
X_train, X_test, y_train, y_test = train_test_split(X_combined, y, test_size=0.2, random_state=42)  
  
# 构建神经网络模型  
model = tf.keras.Sequential([  
    tf.keras.layers.Dense(128, activation='relu', input_shape=(X_train.shape[1],)),  
    tf.keras.layers.Dropout(0.2),  
    tf.keras.layers.Dense(64, activation='relu'),  
    tf.keras.layers.Dense(32, activation='relu'),  
    tf.keras.layers.Dense(1)  
])  
  
# 编译模型  
model.compile(optimizer='adam', loss='mae')  
  
# 训练模型  
model.fit(X_train, y_train, epochs=200, batch_size=32, validation_data=(X_test, y_test))  
  
# 评估模型性能  
loss = model.evaluate(X_test, y_test)  
print("模型在测试集上的损失（MAE）:", loss)

#out